#ifndef MONO_EVENT_TIMERTASK_H_
#define MONO_EVENT_TIMERTASK_H_
#include <mono/common/timestamp.h>
#include <mono/common/atomic.h>
#include <functional>
#include <cstdint>



namespace mono {

typedef std::function<void()> TimerCallback;

class TimerTask {
public:
    TimerTask(const TimerCallback& cb, const mono::Timestamp& when,
                  const int64_t& interval)
      : _callback(cb),
        _expiration(when),
        _interval(interval),
        _repeat(interval > 0),
        _sequence(_gTimerNum++)
    { }

   void run() const
   {
      _callback();
   }

    mono::Timestamp& expiration()  { return _expiration; }
    bool repeat() const { return _repeat; }
    int64_t sequence() const { return _sequence; }

    void restart(const mono::Timestamp &now);

    int64_t step() const { return _interval; }
    
    static int64_t numCreated() { return _gTimerNum.load(); }
private:
    const TimerCallback       _callback;
    Timestamp                 _expiration;
    const int64_t             _interval;
    const bool                _repeat;
    const int64_t             _sequence;

    static std::atomic<int64_t> _gTimerNum;
};

}

#endif
